package CarPackage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/updateQuantity.do")
public class UpdateQuantityServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String goodsId = request.getParameter("goodsId");
        String userId = request.getParameter("userId");
        String action = request.getParameter("action"); // "add" 或 "reduce"

        System.out.println(userId);
        CarDao carDao = new CarDao();
        try {
            if ("add".equals(action)) {
                carDao.updateQuantity(goodsId, userId, 1);
                response.sendRedirect("car.jsp");
            } else if ("reduce".equals(action)) {
                carDao.updateQuantity(goodsId, userId, -1);
                response.sendRedirect("car.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 错误处理
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
